package com.lanou3g.dllo.baidumusic.musiclibrary.recommend.songsrecommenddetail;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/23.
 */
public class SongsRecommendAdapter extends RecyclerView.Adapter<SongsRecommendAdapter.MyViewHolder> {

    private SongsRecommendBean songsRecommendBean;
    private List<SongsRecommendBean.ContentBean.ListBean> list;
    private MyRvOnClickListener myRvOnClickListener;

    public void setSongsRecommendBean(SongsRecommendBean songsRecommendBean) {
        this.songsRecommendBean = songsRecommendBean;
        list = songsRecommendBean.getContent().getList();
        notifyDataSetChanged();
    }

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.context).inflate(R.layout.item_songsrecommend, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        SingleVolley.getSingleVolley(MyApp.context).getImageLoader().get(list.get(position).getPic(),
                ImageLoader.getImageListener(holder.songsRecommendPhotoIv, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        holder.songsRecommendSongsTitle.setText(list.get(position).getTitle());
        holder.songsRecommendListenedCountTv.setText(list.get(position).getListenum());

        if (myRvOnClickListener != null) {
            holder.songsRecommendLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    // 调用接口对象的onClick方法
                    myRvOnClickListener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        LinearLayout songsRecommendLayout;
        ImageView songsRecommendPhotoIv;
        TextView songsRecommendSongsTitle,songsRecommendListenedCountTv;


        public MyViewHolder(View itemView) {
            super(itemView);

            songsRecommendPhotoIv = (ImageView) itemView.findViewById(R.id.songsrecommend_photo_iv);
            songsRecommendSongsTitle = (TextView) itemView.findViewById(R.id.songsrecommend_songs_title);
            songsRecommendListenedCountTv = (TextView) itemView.findViewById(R.id.songsrecommend_listenedcount_tv);
            songsRecommendLayout = (LinearLayout) itemView.findViewById(R.id.songsrecommend_layout);
        }
    }
}
